/**
 * Copyright (c) 2024 myons Inc. All rights reserved.
 */
package cn.zmwh.portal.modules.index.controller;

import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.minio.http.Method;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

public class MinioPresignedUrlGenerator {

    public static void main(String[] args) {
        try {
            // 创建 MinIO 客户端
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://127.0.0.1:9001") // MinIO 服务的 URL
                    .build();

            //{"url":"http://127.0.0.1:9090/api/v1/service-account-credentials","accessKey":"aMIzI1cp2OQ5tKTROTBY","secretKey":"6gSpwsyGS7aD9mjqhqf3Whr4KM4hVFeBRiFPFqgN","api":"s3v4","path":"auto"}
            // 生成预签名 URL 参数
            GetPresignedObjectUrlArgs presignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
                    .method(Method.POST) // 使用 PUT 方法上传文件
                    .bucket("tt-love") // 存储桶名称
                    .object("xxx.png")
                    .expiry(7, TimeUnit.DAYS) // URL 有效期 60 秒
                    .build();

            // 生成预签名 URL
            String presignedUrl = minioClient.getPresignedObjectUrl(presignedObjectUrlArgs);

            System.out.println("Generated Presigned URL: " + presignedUrl);

        } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            System.err.println("Error occurred: " + e);
        }
    }
}

